var P7 = function (game) {
  this.create = function () {

    ClosePage(game, '7_0', '7', '7_r', '7_w');


    function Page(game, cbg, mbg, rbg, wbg) {
      var bg = game.add.sprite(0, 0, mbg);
      bg.anchor.setTo(0, 0);


      var group = game.add.group();
      var bimgs = [
        ['l_off', 'l_on'],
        ['r_off', 'r_on']
      ];
      var flagAry=[0,0];
      var sprite;

      /*var Limg=game.add.sprite(855, 290, bimgs[0][flagAry[0]]);
      sprite.data = i;
      sprite.inputEnabled = true;
      sprite.input.useHandCursor = true;
      sprite.events.onInputDown.add(onClick, this);
      var Rimg=game.add.sprite(928, 185, bimgs[1][flagAry[1]]);*/

      showImg();
      function showImg() {
        group.destroy();
        group = game.add.group();
        for (var i = 0; i < 2; i++) {
          if (i === 0) {
            sprite = game.add.sprite(855, 290, bimgs[i][flagAry[i]]);
          } else {
            sprite = game.add.sprite(928, 185, bimgs[1][flagAry[1]]);
          }
          sprite.data = i;
          sprite.inputEnabled = true;
          sprite.input.useHandCursor = true;
          sprite.events.onInputDown.add(onClick, this);
          group.add(sprite);
        }
      }


      function onClick(arg) {
        flagAry.forEach(function (item, i) {
          if (i != arg.data) {
            flagAry[i] = 0;
          }
        })
        flagAry[arg.data] = flagAry[arg.data] === 0 ? 1 : 0;
        showImg();
      }


      game.add.button(1100, 630, imgs['prev'], onPrev, this, 2, 1, 0);
      game.add.button(1250, 630, imgs['submit'], onSubmit, this, 2, 1, 0);
      function onPrev() {
        game.goState('back');
        group.destroy();
      }
      function onSubmit(arg) {
        if (flagAry.join('_') === '1_0') {
          RightPage(game, cbg, mbg, rbg, wbg);
        } else {
          WrongPage(game, cbg, mbg, rbg, wbg);
        }
        group.destroy();
      }
    }


    function ClosePage(game, cbg, mbg, rbg, wbg) {
      var container = game.add.sprite(0, 0, '');
      var close = game.add.button(0, 0, cbg, onClose, this, 2, 1, 0);
      container.addChild(close);

      function onClose() {
        container.destroy();
        Page(game, cbg, mbg, rbg, wbg);
      }
    }

      function WrongPage(game, cbg, mbg, rbg, wbg) {
          var container = game.add.sprite(0, 0, '');

          var bg = game.add.image(0, 0, wbg);
          container.addChild(bg);

          var next = game.add.button(1250, 630, imgs['retry'], onClick, this, 2, 1, 0);
          container.addChild(next);
          function onClick() {
              container.destroy();
              Page(game, cbg, mbg, rbg, wbg);
          }
      }

    function RightPage(game, cbg, mbg, rbg, wbg) {
      var container = game.add.sprite(0, 0, '');
      var bg = game.add.image(0, 0, rbg);
      container.addChild(bg);
      var next = game.add.button(1250, 630, imgs['next'], onClick, game, 2, 1, 0);
      container.addChild(next);

      function onClick() {
        game.goState('next');
      }
    }

  };

}